<%--
  Created by IntelliJ IDEA.
  User: 27716
  Date: 2024/10/30
  Time: 10:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>角色修改</title>
    <script src="../../static/js/jquery-1.8.2.min.js"></script>
    <link rel="stylesheet" href="../../static/css/css.css">
    <script>
        //获取角色列表 + 该用于已有角色的回显 文档就绪函数
        $(function (){
            getRole();
        })
        function getRole(){
            //1.取值 2.组装 2.ajax
            let uid = $("#uid").val();
            if(!uid) {
                alert("数据不能为空");
                return
            }
            //路径传参 ？
            $.ajax({
                url:"/userRole/findRole?uid="+uid,
                dataType:"json",
                success(res){
                    console.log(res);
                    for (let o of res) {
                        $("#table").append(`
                            <tr>
                                <td>
                                    <input type="checkbox" class="ck" value="\${o.rid}" \${o.flag==true?'checked':''}>
                                </td>
                                <td>\${o.rid}</td>
                                <td>\${o.username}</td>
                                <td>\${o.remark}</td>
                            </tr>
                        `)
                    }
                    $("#table").append(`
                            <tr>
                                <td colspan="100">
                                    <input type="button" value="确定修改" onclick="updRole()">
                                    <input type="button" value="返回" onclick="history.back()">
                                </td>
                            </tr>
                        `)
                },
                error(){
                    alert("服务器错误");
                }
            })
        }
    </script>
</head>
<body>
<input type="hidden" value="${param.uid}" id="uid">
<table id="table">
    <tr>
        <td>
            <input type="checkbox" id="check">
        </td>
        <td>编号</td>
        <td>姓名</td>
        <td>备注</td>
    </tr>
</table>
</body>
<script>
    //全选 全不选
    $(document).on('click',"#check",function (){
        $(".ck").prop("checked",this.checked);
    })
    function updRole(){
        //需求 1个uid， 还需要选中的复选框的值
        let uid= $("#uid").val();
        //获取选中的复选框
        let arr = [];
        $(".ck:checked").each(function (){
            let rid = $(this).val();
            //添加到数组
            arr.push(rid);
        })
        if(!uid || !arr.length){
            alert("数组为空");
            return;
        }
        //组装对象 (一个用户拥有多个角色)
        let obj = {uid,arr};
        $.ajax({
            url:"/userRole/updUserRole",
            type:"post",
            data:{reqInfos:JSON.stringify(obj)},
            dataType: "json",
            success(res) {
                console.log(res);
                if(res === 200){
                    alert("修改成功")
                    location.href = "list.jsp";
                }
            },
            error() {
                alert("服务器错误")
            }
        })
    }
</script>
</html>
